package org.example.java8.lambda;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.function.Supplier;

public class MethodReference2 {
    public static void main(String[] args) {
        List<Apple> list = Arrays.asList(new Apple("red", 150), new Apple("yellow", 170), new Apple("green", 200));
        //Comparator是接口函数
        //list.sort((a1, a2) -> a1.getColor().compareTo(a2.getColor()));
        list.sort(Comparator.comparing(Apple::getColor));
        System.out.println(list);


        //list.stream().forEach(apple -> System.out.println(apple));
        //list.stream().forEach(System.out::println);

        //方法推导：要是静态方法
        int value = Integer.parseInt("123");
        //传入的String返回的int
        Function<String, Integer> function = Integer::parseInt;
        Integer result = function.apply("123");
        //System.out.println(result);

        BiFunction<String, Integer, Character> f2 = String::charAt;
        Character c = f2.apply("hello", 1);
        System.out.println(c);

        //方法推导：使用对象的实例
        String string = new String("hello");
        Function<Integer, Character> f3 = string::charAt;
        Character c2 = f3.apply(4);
        System.out.println(c2);

        //方法推导：构造函数
        Supplier<String> supplier = String::new;
        String s = supplier.get();
        System.out.println(s);

        //方法推导：构造函数，传入2个参数String, Long 返回Apple
        BiFunction<String, Long, Apple> appleBiFunction = Apple::new;
        Apple apple = appleBiFunction.apply("red", 100L);
        System.out.println(apple);

    }
}
